package controller.permission;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import common.entities.Msg;
import common.entities.Page;
import entities.permission.Permission;
import entities.permission.Role;
import entities.permission.RoleAndMenu;
import entities.permission.RoleAndPermission;
import service.permission.PermissionService;
import util.base.CurrentUser;
/**
 * 
 * permissionController
 * @author hxl
 * 2018年11月26日
 */
@RestController
@RequestMapping(value="/permission")
public class PermissionController {
	 @Autowired
	 private PermissionService permissionService;
	 /**
	  * 获得角色
	  * @return
	  */
	 @RequestMapping(value="/getRoles")
	 public List<Role> getRoles(){
		 List<Role> roles=permissionService.getRoles();
		 return roles;
	 }
	 /**
	  * 获得角色分页列表
	  * @param name
	  * @param page
	  * @param rows
	  * @return
	  */
	 @RequestMapping(value="/getRolePage")
	 public Page<Role> getRolePage(
			    @RequestParam(value = "name", defaultValue = "") String name,
				@RequestParam(value = "page", defaultValue = "1") int page,
				@RequestParam(value = "rows", defaultValue = "20") int rows){
		 Page<Role> rolePage=permissionService.getRolePage(name,page,rows);
		 return rolePage;
	 }
	 /**
	  * 获得权限分页列表
	  * @param name
	  * @param page
	  * @param rows
	  * @return
	  */
	 @RequestMapping(value="/getPermissionPage")
	 public Page<Permission> getPermissionPage(
			    @RequestParam(value = "name", defaultValue = "") String name,
				@RequestParam(value = "page", defaultValue = "1") int page,
				@RequestParam(value = "rows", defaultValue = "20") int rows){
		 Page<Permission> permissionPage=permissionService.getPermissionPage(name,page,rows);
		 return permissionPage;
	 }
	 /**
	  * 新增角色
	  * @param role
	  * @return
	  */
	 @RequestMapping(value="/saveRole")
	 @Transactional(rollbackFor=Exception.class)
	 public Msg saveRole(Role role,int type){
		   Msg msg=new Msg("0","新增成功！");
		    //重名检验
		    if(permissionService.isExistRole(role.getId(),role.getName())){
		    	msg.setInfo("角色名重复！");
				msg.setSerialNumber("-1");
				return msg;
		    }
		 if(type==0){
			 permissionService.addRole(role);
		 }else{
			 permissionService.updateRole(role);
			 msg.setSerialNumber("2");
			 msg.setInfo("修改成功！");
		 }
		 return msg;
	 }
	 
	 /**
	  * 新增权限
	  * @param role
	  * @return
	  */
	 @RequestMapping(value="/savePermission")
	 @Transactional(rollbackFor=Exception.class)
	 public Msg savePermission(Permission permission,int type){
		   Msg msg=new Msg("0","新增成功！");
		    //重名检验
		    if(permissionService.isExistPermission(permission.getId(),permission.getName())){
		    	msg.setInfo("权限名重复！");
				msg.setSerialNumber("-1");
				return msg;
		    }
		 if(type==0){
			 permission.setId(UUID.randomUUID().toString());
			 permission.setAdd_person(CurrentUser.getCurrentUser().getName());
			 permission.setAdd_time(new Date());
			 permissionService.addPermission(permission);
		 }else{
			 permission.setUpdate_person(CurrentUser.getCurrentUser().getName());
			 permission.setAdd_time(new Date());
			 permissionService.updatePermission(permission);
			 msg.setSerialNumber("2");
			 msg.setInfo("修改成功！");
		 }
		 return msg;
	 }
	 /**
	  * 批量删除角色
	  * @param ids
	  * @return
	  */
	 @RequestMapping(value="/deleteRoles")
	 @ResponseBody
	 public boolean deleteRoles(@RequestParam(value = "ids")String ids){
		 permissionService.deleteRoles(ids);
		 return true;
	 }
	 
	 
	 /**
	  * 获得角色和菜单关联
	  * @param roleId
	  * @return
	  */
	 @RequestMapping(value="/getRoleAndMenu")
	 @ResponseBody
	 public List<RoleAndMenu> getRoleAndMenu(@RequestParam(value = "roleId")String roleId){
		 List<RoleAndMenu> signs=permissionService.getRoleAndMenu(roleId);
		return signs;
	 }
	 @RequestMapping(value="/getRoleAndPermissions")
	 @ResponseBody
	 public List<RoleAndPermission> getRoleAndPermissions(@RequestParam(value = "roleId")String roleId){
		 List<RoleAndPermission> roleAndPermission=permissionService.getRoleAndPermissions(roleId);
		return roleAndPermission;
	 }
	 
	 /**
	  * 设置菜单
	  * @param roleId
	  * @param checkedMenuId
	  * @return
	  */
	 @RequestMapping(value="/setMenuPermission")
	 @ResponseBody
	 public boolean setMenuPermission(@RequestParam(value = "roleId")String roleId,@RequestParam(value = "checkedMenuId",defaultValue="")String checkedMenuId){
		 permissionService.setMenuPermission(roleId,Arrays.asList(checkedMenuId.split(",")));
		 return true;
	 }
	 /**
	  * 批量删除权限 
	  * @param ids
	  * @return
	  */
	 @RequestMapping(value="/deletePermissions")
	 @ResponseBody
	 public boolean deletePermissions(@RequestParam(value = "ids")String ids){
		 permissionService.deletePermissions(ids);
		 return true;
	 }
	 /**
	  * 设置权限
	  * @param roleId
	  * @param checkedPermissionId
	  * @return
	  */
	 @RequestMapping(value="/setPermission")
	 @ResponseBody
	 public boolean setPermission(@RequestParam(value = "roleId")String roleId,@RequestParam(value = "checkedPermissionId",defaultValue="")String checkedPermissionId){
		 permissionService.setPermission(roleId,Arrays.asList(checkedPermissionId.split(",")));
		 return true;
	 }
}
